/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.lookup;

import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import main.Kasir;
import main.ServicesList;
import model.inventory.Dept;
import utility.Constants;

/**
 *
 * @author jay
 */
public class LookupDeptFrm extends javax.swing.JDialog {

    private DefaultTreeModel treeModel;
    private Dept selectedDept;
    
    public LookupDeptFrm() {
        super(Kasir.getFrame(), true);
        initComponents();
    }

    public Object execute() {
        setLocationRelativeTo(null);
        
        Dept dept = ServicesList.getDeptService().getDept(Constants.MASTER_ITEM_DEPTEMENT);
        DefaultMutableTreeNode top = new DefaultMutableTreeNode(dept);
        loadTree(Constants.MASTER_ITEM_DEPTEMENT, top);
        
        treeModel = new DefaultTreeModel(top);
        tree.setModel(treeModel); 
        
        setVisible(true);
        return selectedDept;
    }
    
    private void getObj() {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        selectedDept = (Dept) node.getUserObject();
        System.out.println(selectedDept.toString());
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tree = new javax.swing.JTree();
        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                treeValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(tree);

        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(121, Short.MAX_VALUE)
                .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnCancel)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOK)
                    .addComponent(btnCancel))
                .addGap(0, 8, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        getObj();
        if(selectedDept!=null){
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "Pilih satu terlebih dahulu!",
                "error",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnOKActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        selectedDept=null;
        dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void treeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeValueChanged
    }//GEN-LAST:event_treeValueChanged

    private void loadTree(int induk, DefaultMutableTreeNode parent) {
        List<Dept> depts = ServicesList.getDeptService().getDeptTree(induk);
        
        if (depts.isEmpty()) {
        
        }else {
        
             for (int i=0;i<depts.size();i++){
                Dept dept = depts.get(i);
                DefaultMutableTreeNode child = new DefaultMutableTreeNode(dept);
                if(parent==null){ 
                    parent = child;
                }else {
                     parent.add(child);
                 }
                
                loadTree(dept.getId(), child);
            }
        }
    }
    
    //addTreeSelectionListener
  //  private class treeListen implements Tree{

        
    
    //}
    
//        @Override public void valueChanged(ListSelectionEvent e) {
//            
//            System.out.println("getSelectionPath "+tree.getSelectionPath().toString());
//            System.out.println("getLastSelectedPathComponent "+tree.getLastSelectedPathComponent().toString());
//            //selectedDept = (Dept) tree.getSelectedPathComponent();//tree.getLastSelectedPathComponent();
//            
//            //if(tree.getLastSelectedPathComponent)
//        }

       
   
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree tree;
    // End of variables declaration//GEN-END:variables
}
